package com.xiebishe.mylibrary.common.pageStatus;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;

import com.xiebishe.mylibrary.R;


public class ErrorView extends LinearLayout {

    private OnClickListener listener;
    private ImageView img_head;
    private TextView tv_tip;
    private LinearLayout linContent;

    public void operateView(ViewGroup parent) {
    }
    public void setLoadingListener(OnClickListener loadingListener) {
        this.listener = loadingListener;
    }

    public ErrorView(Context context) {
        super(context);
        initView();
    }

    public ErrorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public void setErrorViewHead(@DrawableRes int res) {
        if (img_head != null) {
            img_head.setImageResource(res);
        }
    }

    public void setErrorTip(String tip) {
        if (tv_tip != null) {
            tv_tip.setText(tip);
        }
    }

    public void setErrorContentBg(@IdRes int id) {
        if (linContent != null) {
            linContent.setBackgroundColor(id);
        }
    }
    public void initView() {
        LinearLayout mContainer = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.page_error, null);
        operateView(mContainer);
        addView(mContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mContainer.findViewById(R.id.img_errorView_retry).setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(v);
            }
        });
    }
}
